from flask import Flask, render_template, request

app = Flask(__name__)


@app.route('/')
def index():
    return render_template("index.html")


# 项目1 温度转换器
@app.route('/project01/')
def project01():
    return render_template("project01.html")


# 项目2 计算器
@app.route('/project02/')
def project02():
    return render_template("project02.html")


# 计算器功能
@app.route('/calculator/<fun>', methods=["post", "get"])
def calculator(fun):
    if fun == "clear":
        return render_template("project02.html", view="", result="")
    elif fun == "result":
        try:
            result = eval(request.form['view'])
            return render_template('project02.html', result=result, view=request.form['view'])
        except:
            return render_template('project02.html', flog="error")
    else:
        return "404"


# # 此路由判断页面按钮是否被点击，如果被点击则执行相应的函数
# @app.route('/calculator/clear', methods=["post", "get"])
# def clear():
#     return render_template("project02.html", view="", result="")

# 摄氏度转华氏度
@app.route('/project01/temperature1/', methods=["post", "get"])
def Tem1():  # 摄氏度转华氏度
    try:
        c = float(request.form['c1'])
        tem1 = 32 + c * 1.8
        return render_template("project01.html", c1=c, f1=tem1, c2=request.form['c2'], f2=request.form['f2'])
    except:
        return render_template("project01.html", flog="False")


# 华氏度转摄氏度
@app.route('/project01/temperature2/', methods=["post", "get"])
def Tem2():  # 华氏度转摄氏度
    try:
        f = float(request.form['f2'])
        tem2 = (f - 32) / 1.8
        return render_template("project01.html", f2=f, c2=tem2, c1=request.form['c1'], f1=request.form['f1'])
    except:
        return render_template("project01.html", flog="False")


if __name__ == '__main__':
    app.run(debug=True)
